home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / Cookie / Source / CookieController.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.7 KB  |  200 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "CookieController.h"
  5. #import "PreferencesController.h"
  6. #import <appkit/appkit.h>
  7. #import <appkit/Font.h>
  8. #import "CookieFile.h"
  9. #import <defaults.h>
  10. #import <libc.h>
  11.  
  12.  
  13. #define MINWIDTH    200.0
  14. #define MINHEIGHT    150.0
  15.  
  16. NXDefaultsVector mydefaults = {
  17.     { "Status","ALIVE" },
  18.     { "Timer","NO" },
  19.     { "Seconds", "60" },
  20.     { "MainFrame","175 555 533 220" },
  21.     { "NXFont", "Ohlfs" },
  22.     { "NXFontSize","10.0" },
  23.     { NULL } };
  24.  
  25. void RunCookieEntry(DPSTimedEntry te, double timeNow, void *data)
  26. {
  27.   /* we set data to self so we can call this method from the timed entry */
  28.     [(CookieController *)data timedEntry];
  29. }
  30.  
  31. @implementation CookieController
  32.  
  33. - windowWillClose:sender
  34. {
  35.     [NXApp terminate:self];
  36.  
  37.     return self;
  38. }
  39.  
  40. - windowWillResize:window toSize:(NXSize *)size
  41. {
  42.     if (size->width  < MINWIDTH)  size->width = MINWIDTH;
  43.     if (size->height < MINHEIGHT) size->height = MINHEIGHT;
  44.  
  45.     return self;
  46. }
  47.  
  48. - timedEntry
  49. {
  50.     if([prefs flags]&PREF_DEAD)
  51.         [NXApp terminate:self];
  52.  
  53.     if([prefs flags]&PREF_TIMER)
  54.         [self newCookie:self];
  55.  
  56.     return self;
  57. }
  58.  
  59. - newCookie:sender
  60. {
  61.     int count,cookieCount = [((List *)cookieList) count];
  62.     CookieFile *cookieFile;
  63.     Cookie *cookie;
  64.     char text[2048];
  65.     int length = 0,random_size;
  66.  
  67.     // First of all check to see if there are any cookies
  68.     if(!cookieCount)
  69.     {
  70.         NXRunAlertPanel("Fatal Application Error",
  71.          "There are no cookies to display!","OK!",NULL,NULL);
  72.          [NXApp terminate:self];
  73.     }
  74.  
  75.     // The have a look see if there are any selected
  76.     for(count = 0; count < cookieCount; count++)
  77.     {
  78.         cookieFile = [cookieList objectAt:count];
  79.     
  80.         if([cookieFile selected])
  81.             length += [cookieFile filelength];
  82.     }
  83.  
  84.     if(!length)
  85.     {
  86.         NXRunAlertPanel("Application Error",
  87.             "There are no cookies selected to display, please select some","OK!",
  88.             NULL,NULL);
  89.         [prefs startPreferences:self];
  90.         return self;
  91.     }
  92.  
  93.     random_size = random()%length;
  94.  
  95.     for(count = 0,length = 0; count < cookieCount; count++)
  96.     {
  97.         cookieFile = [cookieList objectAt:count];
  98.  
  99.         if(![cookieFile selected])
  100.             continue;
  101.  
  102.         if((random_size >= length) &&
  103.            (random_size <= (length+[cookieFile filelength])))
  104.             break;
  105.  
  106.         length += [cookieFile filelength];
  107.     }
  108.  
  109.     cookieFile = [cookieList objectAt:count];
  110.  
  111.     cookie = [cookieFile pickRandom];
  112.  
  113.     [[titleText setStringValue:[cookie title]] display];
  114.  
  115.     text[0] = 0;
  116.     for(count = 0; count < [[cookie cookieLines] count]; count++)
  117.     {
  118.         strcat(text,[[[cookie cookieLines] objectAt:count] string]);
  119.         strcat(text,"\n");
  120.     }
  121.  
  122.     [[bodyText docView] setText:text];
  123.  
  124.     return self;
  125. }
  126.  
  127. - appDidInit:sender
  128. {
  129.     flags.entry_running = FALSE;
  130.     srandom(time(0));
  131.  
  132.     NXRegisterDefaults([NXApp appName], mydefaults);
  133.  
  134.     [prefs getPreferences];
  135.     [self setWindowPrefs];
  136.  
  137.     [self newCookie:self];
  138.  
  139.     if(!strcmp(NXGetDefaultValue([NXApp appName],"NXAutoLaunch"),"YES"))
  140.         [cookieWindow setFloatingPanel:TRUE];
  141.  
  142.     [cookieWindow makeKeyAndOrderFront:self];
  143.  
  144.     return self;
  145. }
  146.  
  147. - setWindowPrefs
  148. {
  149.     Font *font;
  150.     NXRect myRect;
  151.  
  152.     font = [Font newFont:[prefs fontname] size:[((PreferencesController *)prefs) fontsize]];
  153.     [[bodyText docView] setFont:font];
  154.  
  155.     myRect = [prefs windowframe];
  156.     [cookieWindow placeWindowAndDisplay:&myRect];
  157.  
  158.     return self;
  159. }
  160.  
  161. - newPrefs
  162. {
  163.     if(flags.entry_running)
  164.     {
  165.         DPSRemoveTimedEntry(myTimedEntry);
  166.         flags.entry_running = FALSE;
  167.     }
  168.  
  169.     if(([prefs flags]&PREF_DEAD) || ([prefs flags]&PREF_TIMER))
  170.     {
  171.         myTimedEntry =
  172.             DPSAddTimedEntry((float)[prefs seconds],
  173.                 &RunCookieEntry,self,NX_BASETHRESHOLD);
  174.         flags.entry_running = TRUE;
  175.     }
  176.  
  177.     return self;
  178. }
  179.  
  180. - (float)fontsize
  181. {
  182.     return [[[bodyText docView] font] pointSize];
  183. }
  184.  
  185. - (const char *)fontname
  186. {
  187.     return [[[bodyText docView] font] name];
  188. }
  189.  
  190. - (NXRect)getWindowRect
  191. {
  192.     NXRect myRect;
  193.  
  194.     [cookieWindow getFrame:&myRect];
  195.  
  196.     return myRect;
  197. }
  198.  
  199. @end
  200.